const express = require('express');
const router = express.Router();
const Demo = require('../../service/project_and_wz');
//主页分页查找项目
router.post('/getDemo', async (req, res) => {
    let page = +req.body.page || 1;
    let number = +req.body.number || 10;
    let limits = req.body.limits || '';
    let type = req.body.type || 2; //0代表案例 其他代表文章
    let data = await Demo.selectAll(page, number, limits, type);

    //判断是不是错误报告
    if (Array.isArray(data.rows)) {
        res.send({
            msg: '获取成功！',
            data: {
                data,
                falg: true
            }
        })
    } else {
        res.status(500).send({
            msg: '服务器发生错误！',
            data: {
                data,
                falg: false
            },
        })
    }

});

//查找某个具体项目
router.post('/getIdContent', async (req, res) => {

    let id = +req.body.id;
    let ins = await Demo.getIdInfo(id);
    console.log(ins);
    if (Array.isArray(ins)) {
        res.send({
            msg: '获取成功！',
            data: {
                ins,
                falg: true
            }
        })

    } else {
        res.status(500).send({
            msg: 'error',
            data: {
                ins,
                falg: false
            }
        })
    }
})

//更新观看量
router.post('/lookup', async (req, res) => {
    await Demo.updateLook(req.body.id);
    res.send({
        msg: '成功！',
    })
})

//获取最热文章
router.post('/get_hot_a', async (req, res) => {
    const ins = await Demo.get_hot_a();

    //判断是不是错误报告
    if (Array.isArray(ins)) {
        res.send({
            msg: '获取成功！',
            data: {
                ins,
                falg: true
            }
        })
    } else {
        res.status(500).send({
            msg: '服务器发生错误！',
            data: {
                ins,
                falg: false
            },
        })
    }
})

//获取最热项目
router.post('/get_hot_p', async (req, res) => {
    const ins = await Demo.get_hot_p();
    //判断是不是错误报告
    if (Array.isArray(ins)) {
        res.send({
            msg: '获取成功！',
            data: {
                ins,
                falg: true
            }
        })
    } else {
        res.status(500).send({
            msg: '服务器发生错误！',
            data: {
                ins,
                falg: false
            },
        })
    }
})

//添加文章
router.post('/add_text', async (req, res) => {
    const ins = await Demo.add_txt(req.body.title, req.body.syn, req.body.time, req.body.content, req.body.classifi)
    res.send({
        msg: '成功',
        ins
    })
})

//添加demo
router.post('/add_demo', async (req, res) => {
    const ins = await Demo.add_demo(req.body.title, req.body.syn, req.body.time, req.body.cover, req.body.link, req.body.classifi)
    res.send({
        msg: '成功',
        ins
    })
})


module.exports = router;